/**
 * Created by fupeng on 17/3/12.
 */
/**
 * Created by fupeng on 17/3/9.
 */
var movieFootPrintDao =  require('../dao/MovieFootPrintDao');
var memberDao = require('../dao/MemberDao');
var cdn = 'http://cdn.bestmovie.com.cn/MOVIE/';
var poster = '/POSTER/';
var request = require('request');
var apiurl = require('../../config').FPEsever.movieapiurl;

module.exports = {
    getMovieInfoLimitModel:function(req, callback){
        if (/^\d+$/.test(req.body.lastIndex)) {
            var open_id = req.body.open_id;
            console.log(req.body.lastIndex);
            var requresData = {
                openId:open_id,
                lastIndex:req.body.lastIndex
            };
            if (open_id) {
                request({
                    headers:{
                        'Content-Type':'application/json'
                    },
                    url:apiurl,
                    method:'post',
                    body: JSON.stringify(requresData)

                },function (err, response, body) {
                    if(err){
                        console.log('足迹获取失败'+err)
                        callback({
                            message: 'error',
                            status: 'error',
                            data: '获取足迹失败'
                        });
                        return;
                    }
                    try {
                        movieInfo = eval('('+body+')').data;
                        console.log(body);
                        var sqlfoot ='';
                        movieInfo.forEach(function(val,i){
                            if(i === 0){
                                sqlfoot += ' select *, \''+ val.createtime+ '\' as create_time from movie where mid =  \''+ val.mid +'\'';
                            }else{
                                sqlfoot += ' UNION all select *, \''+val.createtime+ '\' as create_time  from movie where mid = \''+ val.mid +'\'';
                            }

                        });
                        if(sqlfoot){
                            movieFootPrintDao.getMovie({
                                movietable:sqlfoot
                            }, function (data) {
                                if (data.isSuccess) {
                                    data.rows.forEach(function (val,i) {
                                        movieInfo[i] = {
                                            mid: val.mid,                       //影片编号
                                            movie_name: val.movie_name,         //影片名字
                                            director: val.director,              //导演
                                            brief: val.brief,                    //详情介绍
                                            cast: val.cast,                    //演员
                                            score: val.score,                    //评分
                                            country: val.country,                //地区
                                            category: val.category,               //类型-标签
                                            publishdate: val.publishdate,        //影片-年
                                            poster: cdn + val.mid + poster + val.poster,
                                            create_time:val.create_time
                                        };

                                    });
                                    callback({
                                        message: 'success',
                                        status: 'success',
                                        data: movieInfo
                                    });
                                } else {
                                    callback({
                                        message: 'error',
                                        status: 'error',
                                        data: '查询足迹失败'
                                    });
                                }
                            })
                        }else{
                            callback({
                                message: 'success',
                                status: 'success',
                                data: ''
                            });
                        }
                    }  catch (error) {
                    console.log("获取足迹: Error!" + error.stack);
                        callback({
                            message: 'success',
                            status: 'success',
                            data: '足迹 查询 失败'
                        });
                    }
                })
            }else{
                callback({
                    message: 'error',
                    status: 'error',
                    data: 'openid获取失败'
                });
            }
        }else{
            callback({
                message: 'error',
                status: 'error',
                data: '参数有误'
            });
        }
    }
};
